package lc.前缀和;

import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: qian
 * @createDate: 2021/10/20
 */
public class Math560_和为K的子数组 {

    public int subarraySum(int[] nums, int k) {

        Map<Integer, Integer> map = new HashMap<>();
        map.put(0, 1);
        int sum = 0, ret = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
            int res = sum - k;
            if (map.containsKey(res)) {
                ret += map.get(res);
            }
            map.put(sum,map.getOrDefault(sum,0) +1);
        }
        return ret;

    }
}
